package com.platform.framework.security.filter;

import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.platform.common.constant.Constants;
import com.platform.common.core.domain.entity.WebUser;
import com.platform.common.core.domain.model.WebLoginUser;
import com.platform.common.enums.UserTypeEnum;
import com.platform.framework.web.service.admin.TokenService;
import com.platform.framework.web.service.web.UsernamePhoneAuthenticationToken;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import com.platform.common.core.domain.model.LoginUser;
import com.platform.common.utils.SecurityUtils;
import com.platform.common.utils.StringUtils;

/**
 * token过滤器 验证token有效性
 * 
 * @author ruoyi
 */
@Slf4j
@Component
public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
    @Autowired
    private TokenService tokenService;

    @Value("${token.getUserType:userType}")
    private String userType;

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
            throws ServletException, IOException {
        String getUserType = getUserType(request);

        if(UserTypeEnum.WEB_USER.getCode().equals(getUserType)){
            WebLoginUser webLoginUser = tokenService.getWebUser(request);
            if (StringUtils.isNotNull(webLoginUser) && StringUtils.isNull(SecurityUtils.getAuthentication())) {
                tokenService.verifyWebToken(webLoginUser);
                UsernamePhoneAuthenticationToken authenticationToken = new UsernamePhoneAuthenticationToken(webLoginUser, null);
                authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
                SecurityContextHolder.getContext().setAuthentication(authenticationToken);
            }
        }else if (UserTypeEnum.DEVICE_USER.getCode().equals(getUserType)){
        } else if(UserTypeEnum.TENANT_USER.getCode().equals(getUserType)){


        }else{
            LoginUser loginUser = tokenService.getLoginUser(request);
            if (StringUtils.isNotNull(loginUser) && StringUtils.isNull(SecurityUtils.getAuthentication())) {
                tokenService.verifyToken(loginUser);
                UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(loginUser, null, loginUser.getAuthorities());
                authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
                SecurityContextHolder.getContext().setAuthentication(authenticationToken);
            }
        }
        chain.doFilter(request, response);
    }

    /**
     * 获取客户端类型
     * @param request
     * @return
     */
    private String getUserType(HttpServletRequest request){
        String getUserType = request.getHeader(userType);
        return getUserType;
    }
}
